#include "SCI_HAL_QE.h"
#include "RTC_HAL_QE.h"
#include "IIC_HAL_QE.h"
#include "TPM_HAL_QE.h"

/* General Definitions */

#define COMM_TIMEOUT    16
#define COMM_TIMEOUT_VALUE  120

/* VSCI definitions */

#define VSCI_TIMER		2

#define VSCI_TIMER_CHANNEL	1

#define VSCI_RX_PIN		PTHD_PTHD7
#define VSCI_RX_PIN_DD  PTHDD_PTHDD7

#define VSCI_TX_PIN		PTHD_PTHD6
#define VSCI_TX_PIN_DD	PTHDD_PTHDD6

#define VSCI_BAUD_RATE 	50000

#define VSCI_TIMER_CLOCK	8000000

#define VSCI_TIMER_DELAY_VALUE	(uint16)(VSCI_TIMER_CLOCK / VSCI_BAUD_RATE)

 // The adjustment is half that of the GPS manager because the main
 // MCU runs at twice the speed.
#define VSCI_TIMER_DELAY_START_BIT (uint16)(VSCI_TIMER_DELAY_VALUE - 16)
 
#define VSCI_TIMER_HALF_DELAY	(uint16)(VSCI_TIMER_DELAY_VALUE / 2)

/* I2C definitions */

#define COMM_GPS_IIC_CHAN   2

#define COMM_IIC_CALLBACK_ADDR      0xAA

#define GPS_RX_BUFFER_SIZE      11      // Value includes checksum

#define GPS_TX_BUFFER_SIZE      1

#define COMM_GPS_MANAGER_IIC_ADDRESS    0xCC

#define GPS_PACKET_REQUEST      0xAF
#define GPS_SET_HOME_COMMAND    0x50

#define GPS_HOME_FEEDBACK	0xE3

#define COMM_SCI_PACKET_HEADER     0x0F
#define COMM_REQUEST_PACKET    0xF0

/* SCI definitions */
#define COMM_TRANSCEIVER_SCI_CHAN   2

#define SMAC_TX_BUFFER_SIZE     12      // Value does not include checksum

#define SMAC_RX_BUFFER_SIZE     8

#define SCI_TX_FAIL     0x00
#define SCI_TX_SUCCESS  0x01

#define SCI_RX_FAIL     0x00
#define SCI_RX_SUCCESS  0x01

/* Communication request definitions */

#define COMM_REQ_SET_GPS_HOME			0x80
#define COMM_REQ_SET_AILERON_ZERO		0x40
#define COMM_REQ_SET_RUDDER_ZERO		0x20
#define COMM_REQ_SET_ELEVATOR_ZERO		0x10

#define COMM_REQ_SET_ACCEL_ZERO		    0x08
#define COMM_REQ_TURN_ON_OFF_ULTRASONIC 0x04
#define COMM_REQ_TURN_ON_OFF_PLANE		0x02
#define COMM_REQ_TURN_ON_MOTOR			0x01

#define DIR_MOVEMENT_N	0x80
#define DIR_MOVEMENT_S	0x40
#define DIR_MOVEMENT_E	0x10
#define DIR_MOVEMENT_W	0x20

#define DIR_POS_N	0x08
#define DIR_POS_S	0x04
#define DIR_POS_E	0x01
#define DIR_POS_W	0x02

#define STATUS_SHORT_CIRCUIT    0x80
#define STATUS_GPS_FIX_READY    0x40
#define STATUS_MOTOR_OVERHEAT   0x20
#define STATUS_DRIVER_OVERHEAT  0x10
#define STATUS_ULTRASONIC_ON    0x08
#define STATUS_HBLED_ON         0x04
#define STATUS_ACCEL_FAIL       0x02
#define STATUS_AIRPLANE_ON      0x01

#define CONFIG_FEEDBACK_SUCCESS 0xF0
#define CONFIG_FEEDBACK_FAILURE 0xA0

#define CONFIG_FEEDBACK_HOME_POS    0x08
#define CONFIG_FEEDBACK_AILERON     0x04
#define CONFIG_FEEDBACK_RUDDER      0x02
#define CONFIG_FEEDBACK_ELEVATOR    0x01

#define CONFIG_FEEDBACK_ACCEL       0x08
#define CONFIG_FEEDBACK_USOUND      0x04
#define CONFIG_FEEDBACK_PLANE_ON    0x02
#define CONFIG_FEEDBACK_MOTOR_ON    0x01


uint8 u8Comm_RequestSCIPacket (void);

void vfnComm_ReadSCIPacket (void);

uint8 u8Comm_SendSCIPacket (void);

void vfnComm_BuildSCIPacket (void);

void vfnComm_ConfigureIIC (void);

void vfnIIC_Master_Read_and_Store (void);

void vfnIIC_MasterReceive (uint8 u8slave);

void vfnIIC_ServiceModule (void);

void vfnIIC_MasterTransmit (uint8 u8addr, uint8 u8data);

uint8 u8Comm_RequestGPSPacket (void);

uint8 u8Comm_IICReadPacket (void);

uint8 u8Comm_SendGPSCommand (uint8 u8command);

void vfnComm_ConfigureSCI (void);

void vfnVSCI_Configure (void);

uint8 u8VSCI_ReadByte (void);

void vfnVSCI_SendByte (uint8 u8byte);

void vfnComm_VSCIReadPacket (void);